package com.magicode.cms.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.ICommentDao;
import com.magicode.cms.dao.INewsCollectDao;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.NewsCollect;
import com.magicode.cms.entiy.base.NewsComment;
import com.magicode.cms.entiy.base.User;
import com.magicode.cms.service.INewsCollectService;
import com.magicode.common.dao.PageResults;

/**
 * 
 * @author qin.hui 用户收藏
 */
@Repository("newsCollectService")
public class NewsCollectServiceImpl extends
		BaseServiceImpl<NewsCollect, INewsCollectDao> implements
		INewsCollectService, UtilPath {

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		// TODO Auto-generated method stub
		return null;
	}

	@Autowired
	@Qualifier("newsCollectDao")
	private INewsCollectDao dao_newsCollect;

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql = " from " + NewsCollect.class.getName() + " "
				+ entity_alias_name + " where 1=1 ";
		if (!querywhere.isEmpty()) {
			if (querywhere.containsKey("author")) {
				hql = hql + " and " + entity_alias_name + ".author.id='"
						+ querywhere.get("author") + "'";
			}
		}
		String order =" order by  "+ entity_alias_name +".createTime desc";
		return hql+order;
	}

	@Override
	public INewsCollectDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return dao_newsCollect;
	}

	public INewsCollectDao getDao_newsCollect() {
		return dao_newsCollect;
	}

	public void setDao_newsCollect(INewsCollectDao dao_newsCollect) {
		this.dao_newsCollect = dao_newsCollect;
	}

	@Override
	public PageResults Newslist(Map<String, String> prams, Integer pageNo,
			Integer pageSize) {
		// TODO Auto-generated method stub
		System.out.println("----------");
		return this.getDao_newsCollect().Newslist(prams, pageNo, pageSize);

	}
	@Override
	public boolean deleteNewsCollect(String userid, String newid){
		return dao_newsCollect.deleteNewsCollect(userid, newid);
	}
	private boolean cled(String userid, String newsid){
		String hql = " select count(t.id) from "+NewsCollect.class.getName()+" t where t.news.id='"+newsid+"' and t.author.id='"+userid+"'";
		long count = dao_newsCollect.queryCount(hql);
		return 0==count?true:false;
	}
	@Override
	public void addNewscl(String userid, String newsid) {
		try {
			News news = (News) dao_newsCollect.getObject(News.class.getName(),newsid);
					
			if(this.cled(userid, newsid)){
				NewsCollect obj = new NewsCollect();
				User user = (User) dao_newsCollect.getObject(User.class.getName(),
						userid);
				obj.setAuthor(user);
				obj.setNews(news);
				obj.setCreateTime(new Date());
				dao_newsCollect.createObject(obj);
			}
			String sql = " select count(t.id) from "
					+ NewsCollect.class.getName() + " t where t.news.id =:id ";
			Map<String,String> map = new HashMap<String, String>();
			map.put("id", newsid);
			List<Long> list = dao_newsCollect.getCommonDaoService()
					.getListBySQLMap(sql, map);
			if (list != null && list.size() > 0) {
				Long count = list.get(0);
				if (count != null) {
					news.setCollectCount(count.intValue());
					dao_newsCollect.editObject(news);
				}
			}
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	}

}
